<?php

namespace app\modules\admin\services;

use app\common\services\BaseService;
use app\components\pagination\Pagination;
use app\config\Constants;
use app\models\User;
use app\models\UserInfoStore;
use app\models\UserInfoUser;

class InfoService extends BaseService{

    /**
     * 获取信息
     * @return array|null
     * @throws \yii\base\InvalidConfigException
     */
    public static function getInfoById($id){
        return [
            'store'=>UserInfoStore::findOne($id),
            'user'=>UserInfoUser::findOne($id),
        ];
    }

    /**
     * 获取用户认证ID
     * @return array
     */
    public static function getUserIds(){
        return UserInfoUser::find()->select(['user_id'])->column();
    }

    /**
     * 获取企业认证ID
     * @return array
     */
    public static function getStoreIds(){
        return UserInfoStore::find()->select(['user_id'])->column();
    }

    /**
     * 店铺
     * @param $page
     * @param $pagesize
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function store(&$page,$pagesize){
        $query = UserInfoStore::find()
            ->andWhere(['is_verify'=>Constants::VERIFY_WAIT]);
        $page = new Pagination(['totalCount' => $query->count(), 'pageSize' => $pagesize]);
        $query->offset($page->getOffset());
        $query->limit($page->getLimit());

        return $query->all();
    }

    /**
     * 用户
     * @param $page
     * @param $pagesize
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function user(&$page,$pagesize){
        $query = UserInfoUser::find()
            ->andWhere(['is_verify'=>Constants::VERIFY_WAIT]);
        $page = new Pagination(['totalCount' => $query->count(), 'pageSize' => $pagesize]);
        $query->offset($page->getOffset());
        $query->limit($page->getLimit());

        return $query->all();
    }

    /**
     * 审核成功
     * @param $type
     * @param $id
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function success($type,$id){
        $user = User::findOne($id);
        if(!$user){
            self::setError(\Yii::t("app","用户不存在"));
            return false;
        }
        if($type == Constants::TYPE_STORE){
            $model = UserInfoStore::findOne($id);
        }else{
            $model = UserInfoUser::findOne($id);
        }
        if(!$model){
            self::setError(\Yii::t("app","用户不存在"));
            return false;
        }
        $model->setAttribute("is_verify",Constants::VERIFY_SUCCESS);
        $model->setAttribute("verify_msg","审核通过");
        return $model->update() !== false;
    }

    /**
     * 审核失败
     * @param $type
     * @param $id
     * @param $msg
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function fail($type,$id,$msg){
        $user = User::findOne($id);
        if(!$user){
            self::setError(\Yii::t("app","用户不存在"));
            return false;
        }
        if($type == Constants::TYPE_STORE){
            $model = UserInfoStore::findOne($id);
        }else{
            $model = UserInfoUser::findOne($id);
        }
        if(!$model){
            self::setError(\Yii::t("app","用户不存在"));
            return false;
        }
        $model->setAttribute("is_verify",Constants::VERIFY_FAIL);
        $model->setAttribute("verify_msg",$msg);
        return $model->update() !== false;
    }
}